package com.yuri.news.controller;

import java.util.ArrayList;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import com.yuri.news.domain.Result;
import com.yuri.news.entity.Menu;
import com.yuri.news.service.MenuService;

/**
 * 菜单控制器
 * 
 * @author Yuri
 *
 */
// 允许跨域访问
@CrossOrigin
// json形式返回
@ResponseBody
// 控制器
@Controller
public class MenuController {
	// 日志打印
	private final Logger logger = LoggerFactory.getLogger(MenuController.class);

	// 依赖注入
	@Autowired
	MenuService menuService;

	/**
	 * 添加菜单
	 * 
	 * @param menu
	 */
	@RequestMapping(value = "addMenu", method = RequestMethod.POST)
	public Result<Menu> addMenu(Menu menu) {
		// 打印日志
		logger.info("\n 添加菜单addMenu()开始 Menu = " + menu);
		Result<Menu> result = new Result<>();
		try {
			if(menu.getUpMenu() != null && menu.getUpMenu().getId() == -1) {
				menu.setUpMenu(null);
			}
			Menu men = menuService.saveMenu(menu);
			if(men != null) {
				result.setSuccess("添加成功！");
			}else {
				result.setError("添加失败!");
			}
		} catch (Exception e) {
			e.printStackTrace();
			result.setError("发生异常错误！");
		}
		// 返回数据
		logger.info("\n 添加菜单addMenu()结束 Menu = " + menu);
		return result;
	}

	/**
	 * 删除菜单
	 * @param menu
	 */
	@RequestMapping(value = "delMenu", method = RequestMethod.DELETE)
	public Result<Menu> delMenu(Menu menu) {
		// 打印日志
		logger.info("\n 删除菜单delMenu()开始  Menu = " + menu);
		// 返回数据
		Result<Menu> result = new Result<>();
		try {
			if(menuService.deleteMenu(menu)) {
				result.setSuccess("删除成功！");
			}else {
				result.setError("不存在此菜单！");
			}
		} catch (Exception e) {
			e.printStackTrace();
			result.setError("发生异常错误！");
		}
		logger.info("\n 删除菜单delMenu()结束  Menu = " + menu);
		return result;
	}

	/**
	 * 根据id删除菜单
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "delMenuById", method = RequestMethod.DELETE)
	public Result<Menu> delMenuById(Long id) {
		// 打印日志
		logger.info("\n 根据id删除菜单（delMenuById()）开始 id = " + id);

		Result<Menu> result = new Result<>();
		try {
			if(menuService.deleteMenuById(id)) {
				result.setSuccess("删除成功！");
			}else {
				result.setError("删除失败！");
			}
		} catch (Exception e) {
			e.printStackTrace();
			result.setError("发生异常错误！");
		}
		logger.info("\n 根据id删除菜单（delMenuById()）结束 result = " + result);
		return result;
	}

	/**
	 * 查询所有菜单信息
	 * 
	 * @return
	 */
	@RequestMapping(value="findMenuAll", method = RequestMethod.GET)
	public Result<Menu> findMenuAll() {
		logger.info("\n 查询所有菜单信息（findMenuAll()）开始");
		Result<Menu> result = new Result<>();
		try {
			List<Menu> menus = null;
			menus = menuService.listMenuAll();
			
			if(menus != null && menus.size() > 0) {}
			result.setData(menus);
			result.setSuccess("查询成功！");
		} catch (Exception e) {
			e.printStackTrace();
			result.setError("发生异常错误！");
		}
		logger.info("\n 查询所有菜单信息（findMenuAll()）结束 result = " + result);
		return result;
	}
	/**
	 * 根据id查询菜单信息
	 * @param id	要查询的id
	 * @return	符合要求的菜单信息
	 */
	@RequestMapping(value = "findMenuById", method = RequestMethod.GET)
	public Result<Menu> findMenuById(Long id){
		logger.info("\n 根据id查询菜单（findMenuById）开始：id = " + id);
		Result<Menu> result = new Result<>();
		try {
			List<Menu> menus = new ArrayList<>();
			menus.add(menuService.getMenu(id));
			
			if(menus.size() > 0) {
				result.setData(menus);
				result.setSuccess("查询成功！");
			}else {
				result.setError("无此菜单信息！");
			}
		} catch (Exception e) {
			e.printStackTrace();
			result.setError("发生异常错误 ！");
		}
		logger.info("\n 根据id查询菜单（findMenuById）结束：result = " + result);
		return result;
	}
	/**
	 * 更新菜单信息
	 * @param menu
	 * @return
	 */
	@RequestMapping(value = "updateMenu", method = RequestMethod.PUT)
	public Result<Menu> updateMenu(Menu menu){
		logger.info("\n 更新菜单信息开始（updateMenu）：menu = " + menu);
		Result<Menu> result = new Result<>();
		try {
			if(menu.getUpMenu() != null && menu.getUpMenu().getId() == -1) {
				menu.setUpMenu(null);
			}
			if(menuService.updateMenu(menu)) {
				result.setSuccess("更新成功！");
			}else {
				result.setError("更新失败！");
			}
		} catch (Exception e) {
			e.printStackTrace();
			result.setError("发生异常错误！");
		}
		logger.info("\n 更新菜单信息（updateMenu）结束：result = " + result);
		return result;
	}
	
}
